package com.hospital.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.enums.IsDeleteEnum;
import com.hospital.common.exception.RRException;
import com.hospital.common.utils.PageUtils;
import com.hospital.modules.sys.dao.SysRecommenderDao;
import com.hospital.modules.sys.entity.SysRecommenderEntity;
import com.hospital.modules.sys.entity.SysUserEntity;
import com.hospital.modules.sys.entity.dto.RecommenderSearchByKeyDTO;
import com.hospital.modules.sys.service.SysRecommenderService;
import com.hospital.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("sysRecommenderService")
public class SysRecommenderServiceImpl extends ServiceImpl<SysRecommenderDao, SysRecommenderEntity> implements SysRecommenderService {

    @Autowired
    private SysRecommenderDao sysRecommenderDao;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SysRecommenderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", IsDeleteEnum.NO_DELETE.getValue());

        if (ObjectUtil.isNotEmpty(params.get("key"))) {
            wrapper.like("recommender_name", params.get("key"));
        }

        IPage<SysRecommenderEntity> page = sysRecommenderDao.selectPage(new Page<>(Integer.parseInt(params.get("page").toString()), Integer.parseInt(params.get("limit").toString())), wrapper);

        List<SysRecommenderEntity> records = page.getRecords();

        if (CollectionUtil.isNotEmpty(records)) {
            records.stream().forEach(s -> {
                SysUserEntity userEntity = sysUserService.getById(s.getCreateBy());
                s.setCreateByName(userEntity == null ? "--" : userEntity.getUsername());
            });
        }
        return new PageUtils(page);
    }

    @Override
    public void insert(SysRecommenderEntity entity, int userId) {
        entity.setCreateBy(userId);
        entity.setCreateTime(new Date());
        entity.setUpdateBy(userId);
        entity.setUpdateTime(new Date());
        entity.setIsDelete(IsDeleteEnum.NO_DELETE.getValue());
        this.save(entity);
    }

    @Override
    public void modify(SysRecommenderEntity entity, int userId) {
        entity.setUpdateBy(userId);
        entity.setUpdateTime(new Date());
        this.updateById(entity);
    }

    @Override
    public void delete(List<Integer> asList, int userId) {
        List<SysRecommenderEntity> sysRecommenderEntityList = sysRecommenderDao.selectBatchIds(asList);

        if (CollectionUtil.isEmpty(sysRecommenderEntityList)) {
            throw new RRException("查询[" + asList.stream().map(String::valueOf).collect(Collectors.joining(",")) + "] 数据为空, 删除失败");
        }

        sysRecommenderEntityList.stream().forEach(s -> {
            s.setUpdateBy(userId);
            s.setUpdateTime(new Date());
            s.setIsDelete(IsDeleteEnum.IS_DELETE.getValue());
        });

        this.updateBatchById(sysRecommenderEntityList);
    }

    @Override
    public List<RecommenderSearchByKeyDTO> search() {

        QueryWrapper<SysRecommenderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", IsDeleteEnum.NO_DELETE.getValue());

        List<SysRecommenderEntity> sysRecommenderEntities = sysRecommenderDao.selectList(wrapper);

        if (CollectionUtil.isEmpty(sysRecommenderEntities)) {
            return Collections.EMPTY_LIST;
        }

        List<RecommenderSearchByKeyDTO> result = new ArrayList<>();
        sysRecommenderEntities.stream().forEach(s -> {
            result.add(RecommenderSearchByKeyDTO.builder().id(s.getId()).recommenderMobile(s.getRecommenderMobile()).recommenderName(s.getRecommenderName()).build());
        });
        return result;
    }


}
